package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndStatus(ChannelDto channelDto) {
        //判断参数是否为空
        if (channelDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查，没有或错误则给默认值
        channelDto.checkParam();
        IPage<AdChannel> page = new Page<AdChannel>(channelDto.getPage(),channelDto.getSize());

        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //name条件，模糊查询
        if (StringUtils.isNotBlank(channelDto.getName())){
            queryWrapper.like(AdChannel::getName,channelDto.getName());
        }
        //status条件
        if (channelDto.getStatus()!=null){
            queryWrapper.eq(AdChannel::getStatus,channelDto.getStatus());
        }
        // 添加排序
        queryWrapper.orderByAsc(AdChannel::getOrd);

        //封装结果,page复用
        page = page(page,queryWrapper);

        return new PageResponseResult(channelDto.getPage(),channelDto.getSize(),
                page.getTotal(),page.getRecords());
    }

    @Override
    public ResponseResult addAdChannel(AdChannel adChannel) {
        //判断参数,频道名称是否为空
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //判断频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                .eq(AdChannel::getName, adChannel.getName()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //执行新增
        //添加创建时间
        adChannel.setCreatedTime(new Date());
        //初始化状态
        if (adChannel.getStatus()==null){
            adChannel.setStatus(true);
        }

        this.save(adChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        //判断参数,频道id是否为空
        if (adChannel == null || adChannel.getId() ==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //判断名称是否重复
        AdChannel channel = this.getOne(Wrappers.<AdChannel>lambdaQuery()
                .eq(AdChannel::getName, adChannel.getName()));
        if (channel != null && !adChannel.getId().equals(channel.getId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }

        //执行修改
        this.updateById(adChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断频道是否存在
        if (this.getById(id) == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //判断频道状态
        if (this.getById(id).getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }

        this.removeById(id);
        return ResponseResult.okResult();
    }
}
